﻿package com.oyjd.ybx.business.goods.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestUtils;

import com.oyjd.ybx.bean.vo.GoodsInfoVO;
import com.oyjd.ybx.bean.vo.UserVO;
import com.oyjd.ybx.business.goods.service.GoodsService;
import com.oyjd.ybx.business.goods.vo.GoodsInfoExVO;
import com.oyjd.ybx.fw.action.BaseAction;
import com.oyjd.ybx.fw.action.Page;
import com.oyjd.ybx.fw.util.CodeUtil;
import com.oyjd.ybx.fw.util.OutUtil;
import com.oyjd.ybx.pub.Const;

/**
 * 
 * 商品信息Action控制层
 * 
 * @author <b>欧建东<br/>
 *         Email:oyjd@163.com</b>
 * @version 1.0
 */
@Action(value = "/goods")
@Results({ @Result(name = "viewEditList", location = Const.JSP_goods_edit_list), @Result(name = "viewCheckList", location = Const.JSP_goods_check_list), @Result(name = "viewAdd", location = Const.JSP_goods_add), @Result(name = "viewEdit", location = Const.JSP_goods_edit) })
public class GoodsAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 45354347027015039L;

	private static final Log log = LogFactory.getLog(GoodsAction.class);
	private GoodsService service;

	@Autowired
	public void setService(GoodsService service) {
		this.service = service;
	}

	/**
	 * 进入商品信息编辑列表页面
	 * 
	 * @return
	 */
	public String viewEditList() {
		return "viewEditList";
	}

	/**
	 * 进入商品信息审核发布列表页面
	 * 
	 * @return
	 */
	public String viewCheckList() {
		return "viewCheckList";
	}

	/**
	 * 进入商品信息新增页面
	 * 
	 * @return
	 */
	public String viewAdd() {
		return "viewAdd";
	}

	/**
	 * 进入商品信息修改页面
	 * 
	 * @return
	 */
	public String viewEdit() {
		try {
			HttpServletRequest req = getRequest();
			String id = ServletRequestUtils.getStringParameter(req, "id");
			GoodsInfoVO vo = (GoodsInfoVO) service.getGoodsInfoVO(id);
			req.setAttribute("data", vo);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
		}
		return "viewEdit";
	}

	/**
	 * 获取商品信息列表数据
	 * 
	 * @return
	 */
	public String getList() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			UserVO userVO = (UserVO) session.get(CodeUtil.SessionKey);
			Page<GoodsInfoExVO> page = service.getList(new Page<GoodsInfoExVO>(req), userVO.getAccount());
			//
			map.put("count", page.getCount());
			map.put("info", page.getResult());
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 新增商品信息
	 * 
	 * @return
	 */
	public String add() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			String ageId = ServletRequestUtils.getStringParameter(req, "ageId");
			String classId = ServletRequestUtils.getStringParameter(req, "classId");
			String goodsName = ServletRequestUtils.getStringParameter(req, "goodsName");
			String goodsPic = ServletRequestUtils.getStringParameter(req, "goodsPic");
			String recommend = ServletRequestUtils.getStringParameter(req, "recommend");
			String link = ServletRequestUtils.getStringParameter(req, "link");

			UserVO userVO = (UserVO) session.get(CodeUtil.SessionKey);
			service.add(ageId, classId, goodsName, goodsPic, recommend, link, userVO.getAccount());
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 编辑商品信息
	 * 
	 * @return
	 */
	public String edit() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			String goodsId = ServletRequestUtils.getStringParameter(req, "goodsId");
			String ageId = ServletRequestUtils.getStringParameter(req, "ageId");
			String classId = ServletRequestUtils.getStringParameter(req, "classId");
			String goodsName = ServletRequestUtils.getStringParameter(req, "goodsName");
			String goodsPic = ServletRequestUtils.getStringParameter(req, "goodsPic");
			String recommend = ServletRequestUtils.getStringParameter(req, "recommend");
			String link = ServletRequestUtils.getStringParameter(req, "link");
			service.edit(goodsId, ageId, classId, goodsName, goodsPic, recommend, link);
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 删除商品信息
	 * 
	 * @return
	 */
	public String remove() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			String[] cbs = ServletRequestUtils.getStringParameters(req, "cb");
			service.remove(cbs);
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 更改商品信息状态
	 * 
	 * @return
	 */
	public String updateStatus() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			String[] cbs = ServletRequestUtils.getStringParameters(req, "cb");
			int status = ServletRequestUtils.getIntParameter(req, "status");
			UserVO userVO = (UserVO) session.get(CodeUtil.SessionKey);
			service.updateStatus(cbs, status, userVO.getAccount());
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

}
